package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.IndexImg;
import cn.com.dcsgo.dto.IndexImgAddDTO;
import cn.com.dcsgo.dto.IndexImgQueryDTO;
import cn.com.dcsgo.dto.IndexImgUpdateDTO;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.IndexImgService;
import cn.com.dcsgo.vo.IndexImgVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页轮播图相关接口
 *
 * @author Dcsgo
 * @since 2025/10/28 14:08
 */
@RestController
@RequestMapping("admin/indexImg")
@Tag(name = "首页轮播图相关接口")
public class IndexImgController {
    @Resource
    private IndexImgService indexImgService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    @Operation(summary = "分页获取轮播图信息", description = "轮播图信息按排序数逆序排序")
    public Result<Page<IndexImg>> getPage(PageDTO pageDTO, IndexImgQueryDTO indexImgQueryDTO) {
        return Result.success(indexImgService.getPage(pageDTO, indexImgQueryDTO));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    @Operation(summary = "添加轮播图", description = "状态默认为正常")
    public Result<Void> addIndexImg(@Validated @RequestBody IndexImgAddDTO indexImgAddDTO) {
        return Result.handle(indexImgService.save(indexImgAddDTO));
    }

    @GetMapping("info/{imgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    @Operation(summary = "获取轮播图信息", description = "根据轮播图ID获取")
    public Result<IndexImgVO> getIndexImg(@PathVariable Long imgId) {
        return Result.success(indexImgService.getIndexImgVOById(imgId));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    @Operation(summary = "修改轮播图信息")
    public Result<Void> updateIndexImg(@Validated @RequestBody IndexImgUpdateDTO indexImgUpdateDTO) {
        return Result.handle(indexImgService.updateById(indexImgUpdateDTO));
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    @Operation(summary = "删除轮播图", description = "支持批量删除，请求参数为轮播图ID列表")
    public Result<Void> deleteIndexImg(@RequestBody List<Long> imgIds) {
        return Result.handle(indexImgService.removeBatchByIds(imgIds));
    }

    @GetMapping("indexImgs")
    @Operation(summary = "获取超级店铺轮播图",description = "小程序所需端接口")
    public Result<List<IndexImg>> getIndexImgList(){
        return Result.success(indexImgService.getSupperIndexImg());
    }
}
